const minimatch = require('minimatch');
const evaluate = require('./eval');

/**
 * 过滤文件
 * minimatch: 模式匹配字符串
 * filterFiles, 会根据meta信息中的filters文件进行过滤
 * @param {object} files 
 * @param {object} filters 
 * @param {object} data 
 * @param {function} done 
 */
const filter = (files = {}, filters = {}, data, done) => {
    let results = [],
        file_names = Object.keys(files),
        data_filters = Object.entries(filters || {});

    for (const [name_pattern, express] of data_filters) {
        results = minimatch.match(file_names, name_pattern);
        for (const file_name of results) {
            if (evaluate(express, data)) {
                delete files[file_name];
            }
        }
    }

    done();
}

module.exports = filter;